package com.gpnu7_412.yanzhi.controller.management;

import com.gpnu7_412.yanzhi.entity.User;
import com.gpnu7_412.yanzhi.dto.UserMessageDto;
import com.gpnu7_412.yanzhi.service.ManagementService;
import com.gpnu7_412.yanzhi.service.UserService;
import com.gpnu7_412.yanzhi.status.Impl.ResultCode;
import com.gpnu7_412.yanzhi.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @deprecated
 */
@CrossOrigin
@RestController
@RequestMapping("/management/user")
public class UserManagementController {
    @Autowired
    ManagementService managementService;
    @Autowired
    UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //获取所有用户
    @GetMapping("")
    public Result getAllUser() {
        List<User> userList = managementService.getAllUsers();
        HashMap<String, Object> request = new HashMap<>(2);
        request.put("totalCount", userList.size());
        request.put("list", userList);
        return new Result(ResultCode.SUCCESS, "数据获取成功", request);
    }

    //查看用户详情
    @PostMapping("/detail")
    public Result getUserById(@RequestBody Map<String, String> map) {
        UserMessageDto userMessageDto = userService.getUserMessageById(Integer.parseInt(map.get("userId")));
        return new Result(ResultCode.SUCCESS, "数据获取成功", userMessageDto);
    }

    //管理员删除用户
    @DeleteMapping("/delete")
    public Result deleteAllMessage(@RequestBody Map<String, String> map) {
        userService.deleteUser(Integer.parseInt(map.get("userId")));
        return new Result(ResultCode.SUCCESS, "删除成功");
    }

    //添加用户
    @PostMapping("/add")
    public Result addUser(@RequestBody Map<String, String> map) {
        // 获取信息
        User user = new User();
        user.setUserName((String) map.get("userName"));
        user.setEmail((String) map.get("email"));
        user.setPassword((String) map.get("password"));
        user.setAvatar("default.jpg");
        user.setPermission((String) map.get("permission"));
        String verificationCode = (String) map.get("verificationCode");

        // 尝试从 Redis 中取出验证码
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        String storeCode = operations.get(user.getEmail());
        // 验证码校验
        if (storeCode == null || !storeCode.equals(verificationCode)) {
            return new Result(ResultCode.FAILED, "验证码错误");
        }
        // 用户注册
        userService.register(user);
        return new Result(ResultCode.SUCCESS, "注册成功");
    }

    //用户ID搜索
    @PostMapping("/search")
    public Result searchUserById(@RequestBody Map<String, Object> map) {
        String type = (String) map.get("type");
        HashMap<String, Object> request = new HashMap<>(2);
        // 用户ID搜索
        if (type.equals("ID搜索")) {
            Integer userId = (Integer) map.get("content");
            User user = userService.selectUserById(userId);
            request.put("totalCount", 1);
            request.put("list", user);
        }
        // 用户名搜索
        else if (type.equals("用户名搜索")) {
            String userName = (String) map.get("content");
            List<User> userList = userService.selectUserByUserName(userName);
            request.put("totalCount", userList.size());
            request.put("list", userList);
        }
        // 邮箱搜索
        else if (type.equals("邮箱搜索")) {
            String email = (String) map.get("content");
            List<User> userList = userService.selectUserByEmail(email);
            request.put("totalCount", userList.size());
            request.put("list", userList);
        }
        return new Result(ResultCode.SUCCESS, "搜索成功", request);
    }



}
